package com.dom.domweb.modular.controller;

import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.dom.domweb.modular.shiro.util.ShiroUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Controller
@RequestMapping("/")
public class BaseController {

    @RequestMapping(value = "/login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView model = new ModelAndView("login");
        return model;
    }

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView model = new ModelAndView(request.getRequestURI());
        return model;
    }

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
//    @ResponseBody
    public String login(@RequestParam("username") String userName, @RequestParam("password") String password) {
        System.out.println("HomeController.login");
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);

        Subject subject = SecurityUtils.getSubject();


        log.info("==========" + userName + password);
//        token.setRememberMe(rememberMe);

        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return "{\"Msg\":\"您的账号或密码输入错误\",\"state\":\"failed\"}";
        }

        ShiroUser shiroUser = ShiroUtil.getUser();

//        return "{\"Msg\":\"登陆成功\",\"state\":\"success\"}";
        return "index";
//        // 登录失败从request中获取shiro处理的异常信息。
//        // shiroLoginFailure:就是shiro异常类的全类名.
//        Object exception = request.getAttribute("shiroLoginFailure");
//        String msg = "";
//        if (exception != null) {
//            if (UnknownAccountException.class.isInstance(exception)) {
//                System.out.println("账户不存在");
//                msg = "账户不存在或密码不正确";
//            } else if (IncorrectCredentialsException.class.isInstance(exception)) {
//                System.out.println("密码不正确");
//                msg = "账户不存在或密码不正确";
//            } else {
//                System.out.println("其他异常");
//                msg = "其他异常";
//            }
//        }


        // 此方法不处理登录成功,由shiro进行处理.
    }

    @RequestMapping("/doLogout")
//    @ResponseBody
    public ModelAndView logout() {
        ShiroUtil.getSubject().logout();

        ModelAndView model = new ModelAndView("login");
        return model;
    }

    @RequestMapping("/*/*")
    public ModelAndView page(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView model = new ModelAndView(request.getRequestURI());
        return model;
    }
}
